{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Lines To Write String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfLines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #写字符串需要的行数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>我们要把给定的字符串 <code>S</code>&nbsp;从左到右写到每一行上，每一行的最大宽度为100个单位，如果我们在写某个字母的时候会使这行超过了100 个单位，那么我们应该把这个字母写到下一行。我们给定了一个数组&nbsp;<code>widths</code>&nbsp;，这个数组&nbsp;widths[0] 代表 &#39;a&#39; 需要的单位，&nbsp;widths[1] 代表 &#39;b&#39; 需要的单位，...，&nbsp;widths[25] 代表 &#39;z&#39; 需要的单位。</p>\n",
    "\n",
    "<p>现在回答两个问题：至少多少行能放下<code>S</code>，以及最后一行使用的宽度是多少个单位？将你的答案作为长度为2的整数列表返回。</p>\n",
    "\n",
    "<pre>\n",
    "<strong>示例 1:</strong>\n",
    "<strong>输入:</strong> \n",
    "widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\n",
    "S = &quot;abcdefghijklmnopqrstuvwxyz&quot;\n",
    "<strong>输出:</strong> [3, 60]\n",
    "<strong>解释: \n",
    "</strong>所有的字符拥有相同的占用单位10。所以书写所有的26个字母，\n",
    "我们需要2个整行和占用60个单位的一行。\n",
    "</pre>\n",
    "\n",
    "<pre>\n",
    "<strong>示例 2:</strong>\n",
    "<strong>输入:</strong> \n",
    "widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\n",
    "S = &quot;bbbcccdddaaa&quot;\n",
    "<strong>输出:</strong> [2, 4]\n",
    "<strong>解释: \n",
    "</strong>除去字母&#39;a&#39;所有的字符都是相同的单位10，并且字符串 &quot;bbbcccdddaa&quot; 将会覆盖 9 * 10 + 2 * 4 = 98 个单位.\n",
    "最后一个字母 &#39;a&#39; 将会被写到第二行，因为第一行只剩下2个单位了。\n",
    "所以，这个答案是2行，第二行有4个单位宽度。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>注:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>字符串&nbsp;<code>S</code> 的长度在&nbsp;[1, 1000] 的范围。</li>\n",
    "\t<li><code>S</code> 只包含小写字母。</li>\n",
    "\t<li><code>widths</code> 是长度为&nbsp;<code>26</code>的数组。</li>\n",
    "\t<li><code>widths[i]</code>&nbsp;值的范围在&nbsp;<code>[2, 10]</code>。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-lines-to-write-string](https://leetcode.cn/problems/number-of-lines-to-write-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-lines-to-write-string](https://leetcode.cn/problems/number-of-lines-to-write-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\\n\"abcdefghijklmnopqrstuvwxyz\"', '[4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\\n\"bbbcccdddaaa\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\r\n",
    "        total=0\r\n",
    "        rows=1\r\n",
    "        for c in s:\r\n",
    "            nxt=widths[ord(c)-ord('a')]\r\n",
    "            if total+nxt>100:\r\n",
    "                rows+=1\r\n",
    "                total=0\r\n",
    "            total+=nxt\r\n",
    "        return [rows,total]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], S: str) -> List[int]:\n",
    "        map_dic=dict(zip(\"abcdefghijklmnopqrstuvwxyz\",widths))         \n",
    "        row,col=1,0         \n",
    "        for s in S:             \n",
    "            col+=map_dic[s]             \n",
    "            if col>100:                 \n",
    "                row+=1                 \n",
    "                col=map_dic[s]         \n",
    "        return [row,col]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        reminder = 0\n",
    "        rowNum = 1\n",
    "        for i in s:\n",
    "            reminder += widths[ord(i) - ord(\"a\")]\n",
    "            if reminder > 100:\n",
    "                reminder = widths[ord(i) - ord(\"a\")]\n",
    "                rowNum += 1\n",
    "        return [rowNum, reminder]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        MAX_WIDTH = 100\n",
    "        lines, width = 1, 0\n",
    "        for c in s:\n",
    "            need = widths[ord(c) - ord('a')]\n",
    "            width += need\n",
    "            if width > MAX_WIDTH:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        col = 0\n",
    "        sumc = 0\n",
    "        for c in s:\n",
    "            sumc += widths[ord(c) - ord('a')]\n",
    "            if sumc > 100:\n",
    "                col += 1\n",
    "                sumc = widths[ord(c) - ord('a')]\n",
    "        return [col+1, sumc]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        line_space = 100\n",
    "        line_count = 1\n",
    "        for ss in s:\n",
    "            count = widths[ord(ss)-ord('a')]\n",
    "            if line_space - count >= 0:\n",
    "                line_space -= count\n",
    "            else:\n",
    "                line_count += 1\n",
    "                line_space = 100 - count\n",
    "        return line_count,100-line_space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        MAX_WIDTH = 100\n",
    "        lines, width = 1, 0\n",
    "        for c in s:\n",
    "            need = widths[ord(c) - ord('a')]\n",
    "            width += need\n",
    "            if width > MAX_WIDTH:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        lines,  res = 1, 0\n",
    "        cur_width = 0\n",
    "        for ch in s:\n",
    "            idx = ord(ch) - ord('a')\n",
    "            if cur_width + widths[idx] > 100:\n",
    "                lines += 1\n",
    "                cur_width = 0\n",
    "            \n",
    "            cur_width += widths[idx]\n",
    "        \n",
    "        return lines, cur_width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        n1,n2 = 1,0\n",
    "        for i in s:\n",
    "            if n2 + widths[ord(i)-97] > 100:\n",
    "                n1 += 1\n",
    "                n2 = widths[ord(i)-97]\n",
    "            else:\n",
    "                n2 += widths[ord(i)-97]\n",
    "        return [n1,n2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        a=0\n",
    "        b=c=0\n",
    "        for i in range(len(s)):\n",
    "            if widths[ord(s[i])-ord(\"a\")]+a<=100:\n",
    "                a=widths[ord(s[i])-ord(\"a\")]+a\n",
    "            else:\n",
    "                b=b+1\n",
    "                a=0\n",
    "                a=widths[ord(s[i])-ord(\"a\")]+a\n",
    "        return [b+1,a]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        x,y = 1,0\n",
    "        for i in s:\n",
    "            tmp = widths[ord(i)-97]\n",
    "            if y + tmp > 100:\n",
    "                x += 1\n",
    "                y = tmp\n",
    "            else:\n",
    "                y += tmp\n",
    "        return [x,y]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: list[int], s: str) -> list[int]:\n",
    "        length, row = 0, 1\n",
    "        for ch in s:\n",
    "            width = widths[ord(ch) - ord(\"a\")]\n",
    "            if length + width > 100:\n",
    "                length = width\n",
    "                row += 1\n",
    "            else:\n",
    "                length += width\n",
    "        return [row, length]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        max_width = 100\n",
    "        lines, width = 1, 0\n",
    "        for c in s:\n",
    "            need = widths[ord(c) - ord('a')]\n",
    "            width += need\n",
    "            if width > max_width:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        MAX_WIDTH = 100\n",
    "        lines, width = 1, 0\n",
    "        for c in s:\n",
    "            need = widths[ord(c) - ord('a')]\n",
    "            width += need\n",
    "            if width > MAX_WIDTH:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        row, count = 1, 0\n",
    "        for ch in s:\n",
    "            count += widths[ord(ch)-ord('a')]\n",
    "            if count > 100:\n",
    "                row += 1\n",
    "                count = widths[ord(ch)-ord('a')]\n",
    "        return [row, count]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        count = 0\n",
    "        line =1\n",
    "        for i in s:\n",
    "            count += widths[ord(i)-97]\n",
    "            if count >100:\n",
    "                count = widths[ord(i)-97]\n",
    "                line +=1\n",
    "        return [line,count]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        num_lines = 0\n",
    "        index = 100\n",
    "        for char in s:\n",
    "            length = widths[ord(char)-ord('a')]\n",
    "            if length + index > 100:\n",
    "                num_lines += 1\n",
    "                index = length\n",
    "            else:\n",
    "                index += length\n",
    "        return [num_lines, index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "MAX_w = 100\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans, cur = 1,0\n",
    "        for c in s:\n",
    "            wid = widths[ord(c)-ord('a')]\n",
    "            if cur + wid >MAX_w:\n",
    "                ans += 1\n",
    "                cur = wid\n",
    "            else:\n",
    "                cur += wid\n",
    "        return [ans, cur]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        row = 0\n",
    "        unit = 0\n",
    "        for i in s:\n",
    "            size = widths[ord(i) - ord('a')]\n",
    "            unit += size \n",
    "            while unit > 100:\n",
    "                unit = size\n",
    "                row += 1\n",
    "        return [row + 1, unit]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        # characterlist = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "        # widthmap = {}\n",
    "        # for i in range(26):\n",
    "        #     widthmap[characterlist[i]] = widths[i]\n",
    "        # line = 1\n",
    "        # count = 0\n",
    "        # for ch in s:\n",
    "        #     if count + widthmap[ch] > 100:\n",
    "        #         # 换一行，此时count重置为下一行第一个字符\n",
    "        #         line += 1\n",
    "        #         count = widthmap[ch]\n",
    "        #     else:\n",
    "        #         count += widthmap[ch]\n",
    "        # return [line, count]\n",
    "        lines, width = 1, 0\n",
    "        for ch in s:\n",
    "            need = widths[ord(ch) - ord('a')]\n",
    "            width += need\n",
    "            if width > 100:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ret = []\n",
    "        rownum = 0\n",
    "        row = 0 \n",
    "        for c in s:\n",
    "            w = widths[ord(c) - ord('a')]\n",
    "            if rownum <= 100 and rownum + w > 100:\n",
    "                rownum = w\n",
    "                row += 1\n",
    "            else:\n",
    "                rownum += w\n",
    "        \n",
    "        return [row + 1, rownum] \n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        mp,a,b={chr(97+i):widths[i] for i in range(26)},0,100\n",
    "        for i in s:\n",
    "            if(b+mp[i]>100):a+=1;b=mp[i]\n",
    "            else:b+=mp[i]\n",
    "        return [a,b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        counts=0\n",
    "        countline=1\n",
    "        for i in range(len(s)):\n",
    "            #print(ord(s[i]-ora('a')))\n",
    "            if counts < 100 and counts + (widths[ord(s[i])-ord('a')]) > 100:                                \n",
    "                countline+=1\n",
    "                counts=(widths[ord(s[i])-ord('a')])\n",
    "            elif counts < 100 and counts + (widths[ord(s[i])-ord('a')]) == 100:\n",
    "                if i==len(s)-1:\n",
    "                    return[countline,counts+(widths[ord(s[i])-ord('a')])]\n",
    "                else:\n",
    "                    countline+=1\n",
    "                    counts=0\n",
    "            else:\n",
    "                counts+=widths[ord(s[i])-ord('a')]\n",
    "        return [countline,counts]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        count = 1\n",
    "        length = 0\n",
    "        for str in s:\n",
    "            le = widths[ord(str) - 97]\n",
    "            if length + le <= 100:\n",
    "                length = length + le\n",
    "            else:\n",
    "                length = le\n",
    "                count = count + 1\n",
    "        return [count,length]\n",
    "'''  \n",
    "          \n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        MAX_WIDTH = 100\n",
    "        lines, width = 1, 0\n",
    "        for c in s:\n",
    "            need = widths[ord(c) - ord('a')]\n",
    "            width += need\n",
    "            if width > MAX_WIDTH:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        row = 1\n",
    "        lastRow = 0\n",
    "        for c in s:\n",
    "            idx = ord(c)-ord('a')\n",
    "            lastRow = lastRow + widths[idx]\n",
    "            if lastRow > 100:\n",
    "                lastRow = widths[idx]\n",
    "                row += 1\n",
    "        return [row, lastRow]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        line,pixel = 1,0\n",
    "        for c in s :\n",
    "            w = widths[ord(c) - ord('a')]\n",
    "            if pixel + w <= 100 :\n",
    "                pixel += w\n",
    "            else :\n",
    "                line += 1\n",
    "                pixel = w\n",
    "        return [line,pixel]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        a={c:i for i,c in enumerate('abcdefghijklmnopqrstuvwxyz')}\n",
    "        b,c=1,0\n",
    "        for i in s:\n",
    "            c+=widths[a[i]]\n",
    "            if c>100:\n",
    "                b+=1\n",
    "                c=widths[a[i]]\n",
    "        return [b,c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        # 我们要把给定的字符串 S 从左到右写到每一行上\n",
    "        # 每一行的最大宽度为100个单位，\n",
    "        # 如果我们在写某个字母的时候会使这行超过了100 个单位，那么我们应该把这个字母写到下一行\n",
    "        # 数组 widths[0] 代表 'a' 需要的单位， widths[1] 代表 'b' 需要的单位，...， widths[25] 代表 'z' 需要的单位\n",
    "        # 先排除特殊情况\n",
    "        if not s:\n",
    "            return [0,0]\n",
    "        temp_num = 0  # 用来记录最后一行的单位数\n",
    "        lines = 1\n",
    "        for word in s:\n",
    "            ind = ord(word) - ord('a')\n",
    "            width = widths[ind]\n",
    "            if temp_num + width <= 100:\n",
    "                temp_num += width\n",
    "            else:\n",
    "                lines += 1\n",
    "                temp_num = width\n",
    "        return [lines,temp_num]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\r\n",
    "        row = 1\r\n",
    "        length = 0\r\n",
    "        for c in s:\r\n",
    "            length += widths[ord(c)-ord('a')]\r\n",
    "            if length > 100:\r\n",
    "                row += 1\r\n",
    "                length = widths[ord(c)-ord('a')]\r\n",
    "        return [row, length]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\r\n",
    "        base_index = ord('a')\r\n",
    "        row_num = 1\r\n",
    "        current_len = 0\r\n",
    "        for item in s:\r\n",
    "            item_length = widths[ord(item)-base_index]\r\n",
    "            if current_len + item_length > 100:\r\n",
    "                row_num += 1\r\n",
    "                current_len = item_length\r\n",
    "            else:\r\n",
    "                current_len += item_length\r\n",
    "        return [row_num, current_len]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        c=0\n",
    "        a=1\n",
    "        for i in s:\n",
    "            c+=widths[ord(i)-97]\n",
    "            if c>100:\n",
    "                a+=1\n",
    "                c=widths[ord(i)-97]\n",
    "        return [a,c%100] if c%100!=0 else [a,100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        count = 0\n",
    "        line = 1\n",
    "        for c in s:\n",
    "            if count + widths[ord(c) - ord('a')] > 100:\n",
    "                line += 1\n",
    "                count = widths[ord(c) - ord('a')]\n",
    "            # elif count + widths[ord(c) - ord('a')] == 100:\n",
    "            #     line += 1\n",
    "            #     count = 0\n",
    "            else:\n",
    "                count += widths[ord(c) - ord('a')]\n",
    "        return [line, count]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        row, max_width = 1, 0\n",
    "        for char in s:\n",
    "            char_width = widths[ord(char) - ord('a')]\n",
    "            if char_width + max_width > 100:\n",
    "                row += 1\n",
    "                max_width = char_width\n",
    "            else:\n",
    "                max_width += char_width\n",
    "        return [row, max_width]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        res , row = 0 , 1\n",
    "        for c in s:\n",
    "            res += widths[ord(c) - ord('a')]\n",
    "            \n",
    "            if res > 100:\n",
    "                row += 1\n",
    "                res = widths[ord(c) - ord('a')]\n",
    "        return [row , res]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "class Solution:\r\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\r\n",
    "        res = [1, 0]\r\n",
    "        for c in s:\r\n",
    "            w = widths[ord(c) - ord('a')]\r\n",
    "            if w + res[1] > 100:\r\n",
    "                res[0] += 1\r\n",
    "                res[1] = w\r\n",
    "            else:\r\n",
    "                res[1] += w\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        characterlist = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "        widthmap = {}\n",
    "        for i in range(26):\n",
    "            widthmap[characterlist[i]] = widths[i]\n",
    "        line = 1\n",
    "        count = 0\n",
    "        for ch in s:\n",
    "            if count + widthmap[ch] > 100:\n",
    "                # 换一行，此时count重置为下一行第一个字符\n",
    "                line += 1\n",
    "                count = widthmap[ch]\n",
    "            else:\n",
    "                count += widthmap[ch]\n",
    "        return [line, count]\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        curline=0\n",
    "        line=1\n",
    "        for i in s:\n",
    "            curwid=widths[ord(i)-ord('a')]\n",
    "            if 100-curline>=curwid:\n",
    "                curline+=curwid\n",
    "            else:\n",
    "                line+=1\n",
    "                curline=curwid\n",
    "        return [line,curline]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans = 0\n",
    "        cur = 0\n",
    "        for c in s:\n",
    "            tmp = widths[ord(c) - ord('a')]\n",
    "            if tmp + cur > 100:\n",
    "                ans += 1\n",
    "                cur = 0\n",
    "            cur += tmp\n",
    "        if cur > 0: ans += 1\n",
    "        return [ans, cur]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        re = [0,0]\n",
    "        tmp = 0\n",
    "        t = 0\n",
    "        for l in s:\n",
    "            tmp += widths[ord(l)-ord('a')]\n",
    "            if tmp == 100:\n",
    "                re[0] += 1\n",
    "                t = tmp\n",
    "                tmp = 0\n",
    "            elif tmp > 100:\n",
    "                re[0] += 1\n",
    "                tmp = widths[ord(l)-ord('a')]\n",
    "        if tmp != 0:\n",
    "            re[0] += 1\n",
    "            re[1] = tmp\n",
    "        if re[0] == 1 and tmp == 0:\n",
    "            re[1] = t\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        numLines = 1\n",
    "        runningWidth = 0\n",
    "\n",
    "        for c in s:\n",
    "            width = widths[ord(c) - ord('a')]\n",
    "            if runningWidth + width <= 100:\n",
    "                runningWidth += width\n",
    "            else:\n",
    "                numLines += 1\n",
    "                runningWidth = width\n",
    "\n",
    "        return [numLines, runningWidth]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        wid = 0\n",
    "        row = 1\n",
    "        for c in map(ord, s):\n",
    "            c -= 97\n",
    "            if wid + widths[c] > 100:\n",
    "                row += 1\n",
    "                wid = widths[c]\n",
    "            else:\n",
    "                wid += widths[c]\n",
    "        return [row, wid]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        sum_s = 0\n",
    "        count = 1\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if sum_s <= 100:\n",
    "                sum_s += widths[ord(s[i]) - ord('a')]\n",
    "                i += 1\n",
    "            else:\n",
    "                sum_s = 0\n",
    "                count += 1\n",
    "                i -= 1\n",
    "        if sum_s > 100:\n",
    "            count += 1\n",
    "            sum_s = widths[ord(s[-1]) - ord('a')]\n",
    "        return [count, sum_s]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        cnt = 0\n",
    "        col = 0\n",
    "        for i in s:\n",
    "            cnt += widths[ord(i)-ord('a')]\n",
    "            if cnt > 100:\n",
    "                col += 1\n",
    "                cnt = widths[ord(i)-ord('a')]\n",
    "            elif cnt == 100:\n",
    "                col += 1\n",
    "                cnt = 0\n",
    "\n",
    "        if cnt > 0 and cnt < 100:\n",
    "            col += 1\n",
    "        elif cnt == 0:\n",
    "            cnt = 100\n",
    "        return [col, cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        lineBlank: int = 100\n",
    "        lines: int = 1\n",
    "        for i in range(len(s)):\n",
    "            w = ord(s[i]) - ord('a')\n",
    "            if lineBlank < widths[w]:\n",
    "                lines += 1\n",
    "                lineBlank = 100\n",
    "            lineBlank -= widths[w]\n",
    "\n",
    "        return [lines, 100 - lineBlank]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        x=1\n",
    "        y=0\n",
    "        for c in s:\n",
    "            t=ord(c)-ord('a')\n",
    "            y+=widths[t]\n",
    "            if y>100:\n",
    "                x+=1\n",
    "                y=widths[t]\n",
    "        return [x,y]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        sum_bit = 0 \n",
    "        bit = 0\n",
    "        for i in s:\n",
    "            if 100 - sum_bit >= widths[ord(i) - 97]:\n",
    "                sum_bit += widths[ord(i) - 97]\n",
    "            else:\n",
    "                sum_bit = 100 + widths[ord(i) - 97]\n",
    "            if sum_bit > 100:\n",
    "                sum_bit -= 100\n",
    "                bit += 1\n",
    "        return [bit + 1, sum_bit]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        dic={}\n",
    "        n=len(widths)\n",
    "        dd={}\n",
    "        for i in range(n):\n",
    "            dic[chr(i+ord('a'))]=widths[i]\n",
    "            dd[i]=widths[i]\n",
    "        m=len(s)\n",
    "        cur=0\n",
    "        lines=0\n",
    "        for ch in s:\n",
    "            num=dic[ch]\n",
    "            cur+=num\n",
    "            if cur >100:\n",
    "                lines+=1\n",
    "                # 如果本行的值+num 之后>100，那就换行，下一行的初始值就是 num\n",
    "                cur=num\n",
    "        return [lines+1,cur]\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        dic = {}\n",
    "        for i in range(len(widths)):\n",
    "            key=chr(i+ord('a'))\n",
    "            dic[key]=widths[i]\n",
    "        print(dic)\n",
    "        lines=1\n",
    "        cur=0\n",
    "        for c in s:\n",
    "            # num=widths[ord(c)-ord('a')]\n",
    "            num=dic[c]\n",
    "            if cur+num>100:\n",
    "                lines+=1\n",
    "                cur=num\n",
    "            else:\n",
    "                cur=cur+num\n",
    "        return [lines,cur]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans,row=0,1\n",
    "        for ch in s:\n",
    "            temp = widths[ord(ch)-97]\n",
    "            ans+=temp\n",
    "            if ans>100:\n",
    "                row+=1\n",
    "                ans = temp\n",
    "        return [row,ans]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        MAX_WIDTH = 100\n",
    "        lines, width = 1, 0\n",
    "        for c in s:\n",
    "            need = widths[ord(c)-ord('a')]\n",
    "            width += need\n",
    "            if width > MAX_WIDTH:\n",
    "                lines += 1\n",
    "                width = need\n",
    "        return [lines, width]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        cur_len=0\n",
    "        full_row_count=0\n",
    "        for c in s:\n",
    "            l=widths[ord(c)-97]\n",
    "            if cur_len+l<=100:\n",
    "                cur_len+=l\n",
    "            else:\n",
    "                full_row_count+=1\n",
    "                cur_len=l\n",
    "        return [full_row_count+1,cur_len]\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        length = 0\n",
    "        rowlen = 1\n",
    "        i=0\n",
    "        list1=[]\n",
    "        while i<len(s):\n",
    "            #print(i,widths[ord(s[i])-97])\n",
    "            length+=widths[ord(s[i])-97]\n",
    "            if(length>100):\n",
    "                length=0\n",
    "                i-=1\n",
    "                rowlen+=1\n",
    "            i+=1\n",
    "        list1.append(rowlen)\n",
    "        list1.append(length)\n",
    "        return list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans = [1, 0]\n",
    "        for i in range(len(s)):\n",
    "            ans[1] += widths[ord(s[i]) - ord(\"a\")]\n",
    "            if ans[1] < 100:\n",
    "                continue\n",
    "            if ans[1] == 100:\n",
    "                ans[0] += 1\n",
    "                ans[1] = 0\n",
    "                continue\n",
    "            if ans[1] > 100:\n",
    "                ans[0] += 1\n",
    "                ans[1] = widths[ord(s[i]) - ord(\"a\")]\n",
    "                continue\n",
    "        if ans[1] == 0:\n",
    "            ans[0] -= 1\n",
    "            ans[1] = 100\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans = 0\n",
    "        res = 1\n",
    "        for i in s:\n",
    "            if ans + widths[ord(i)-97] <= 100:\n",
    "                ans += widths[ord(i)-97]\n",
    "            else:\n",
    "                ans = 0\n",
    "                res += 1\n",
    "                ans += widths[ord(i)-97]\n",
    "        return [res, ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        lines, pixels = 1, 0\n",
    "        \n",
    "        for char in s:\n",
    "            width = widths[ord(char) - ord('a')]\n",
    "\n",
    "            if pixels + width > 100:\n",
    "                lines += 1\n",
    "                pixels = 0\n",
    "            \n",
    "            pixels += width\n",
    "        \n",
    "        return [lines, pixels]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        lines, width = 1, 0\n",
    "        for i in s:\n",
    "            width += widths[ord(i)-ord('a')]\n",
    "            if width > 100:\n",
    "                lines += 1\n",
    "                width = widths[ord(i)-ord('a')]\n",
    "        return [lines, width]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        tmp_char = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "        char_idx = {j: i for i, j in enumerate(tmp_char)}\n",
    "        tmp_len, line_idx = 0, 0\n",
    "        f = False\n",
    "        for c in s:\n",
    "            f = False\n",
    "            if tmp_len + widths[char_idx[c]] > 100:\n",
    "                line_idx += 1\n",
    "                tmp_len = widths[char_idx[c]]\n",
    "            elif tmp_len + widths[char_idx[c]] == 100:\n",
    "                tmp_len = 0\n",
    "                line_idx += 1\n",
    "                f = True\n",
    "            else:\n",
    "                tmp_len += widths[char_idx[c]]\n",
    "        if tmp_len == 0:\n",
    "            if f:\n",
    "                return [line_idx, 100]\n",
    "            else:\n",
    "                return [line_idx, tmp_len]\n",
    "        else:\n",
    "            return [line_idx + 1, tmp_len]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        dic={}\n",
    "        n=len(widths)\n",
    "        dd={}\n",
    "        for i in range(n):\n",
    "            dic[chr(i+ord('a'))]=widths[i]\n",
    "            dd[i]=widths[i]\n",
    "        m=len(s)\n",
    "        cur=0\n",
    "        lines=0\n",
    "        for ch in s:\n",
    "            num=dic[ch]\n",
    "            cur+=num\n",
    "            if cur >100:\n",
    "                lines+=1\n",
    "                # 如果本行的值+num 之后>100，那就换行，下一行的初始值就是 num\n",
    "                cur=num\n",
    "        return [lines+1,cur]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        temp = 0\n",
    "        row = 1\n",
    "        for wd in s:\n",
    "            if temp + widths[ord(wd) - ord('a')] > 100:\n",
    "                temp = widths[ord(wd) - ord('a')]\n",
    "                row += 1\n",
    "            else:\n",
    "                temp += widths[ord(wd) - ord('a')]\n",
    "        return [row, temp]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        dict1 = {key: value for key, value in zip(\n",
    "            'abcdefghijklmnopqrstuvwxyz', range(26))}\n",
    "        lines1 = 1\n",
    "        widthsLeft = 100\n",
    "        for char in s:\n",
    "            curWidth = widths[dict1[char]]\n",
    "            if widthsLeft >= curWidth:\n",
    "                widthsLeft -= curWidth\n",
    "            else:\n",
    "                widthsLeft = 100\n",
    "                widthsLeft -= curWidth\n",
    "                lines1 += 1\n",
    "        return [lines1, 100-widthsLeft]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "MAX_WIDTH = 100\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans, cur = 1, 0\n",
    "        for c in s:\n",
    "            width = widths[ord(c) - ord('a')]\n",
    "            if cur + width > MAX_WIDTH:\n",
    "                ans += 1\n",
    "                cur = width\n",
    "            else:\n",
    "                cur += width\n",
    "        return [ans, cur]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        curline=0\n",
    "        line=1\n",
    "        for i in s:\n",
    "            curwid=widths[ord(i)-ord('a')]\n",
    "            if 100-curline>=curwid:\n",
    "                curline+=curwid\n",
    "            else:\n",
    "                line+=1\n",
    "                curline=curwid\n",
    "        return [line,curline]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        cnt = 0\n",
    "        col = 0\n",
    "        for i in s:\n",
    "            cnt += widths[ord(i)-ord('a')]\n",
    "            if cnt > 100:\n",
    "                col += 1\n",
    "                cnt = widths[ord(i)-ord('a')]\n",
    "            elif cnt == 100:\n",
    "                col += 1\n",
    "                cnt = 0\n",
    "\n",
    "        if cnt > 0 and cnt < 100:\n",
    "            col += 1\n",
    "        elif cnt == 0:\n",
    "            cnt = 100\n",
    "        return [col, cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        MAX_WIDTH = 100\n",
    "        ans, cur = 1, 0\n",
    "        for c in s:\n",
    "            width = widths[ord(c) - ord('a')]\n",
    "            if cur + width > MAX_WIDTH:\n",
    "                ans += 1\n",
    "                cur = width\n",
    "            else:\n",
    "                cur += width\n",
    "        return [ans, cur]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        temp = 0\n",
    "        row = 1\n",
    "        for ch in s:\n",
    "            if widths[ord(ch) - 97] + temp <= 100:\n",
    "                temp += widths[ord(ch) - 97]\n",
    "            else:\n",
    "                temp = widths[ord(ch) - 97]\n",
    "                row += 1\n",
    "        return [row, temp]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        line = 1\n",
    "        lth = 0\n",
    "        for i in s:\n",
    "            width = widths[ord(i)-ord('a')]\n",
    "            if width+lth>100:\n",
    "                line+=1\n",
    "                lth=0\n",
    "            lth+=width\n",
    "        return [line,lth]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans = [1, 0]\n",
    "        for i in range(len(s)):\n",
    "            ans[1] += widths[ord(s[i]) - ord(\"a\")]\n",
    "            if ans[1] <= 100:\n",
    "                continue\n",
    "            if ans[1] > 100:\n",
    "                ans[0] += 1\n",
    "                ans[1] = widths[ord(s[i]) - ord(\"a\")]\n",
    "                continue\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        cnt = 0\n",
    "        ans = 1\n",
    "        for c in s:\n",
    "            pos = ord(c) - ord('a')\n",
    "            # pos -= 97\n",
    "            if cnt + widths[pos] > 100:\n",
    "                cnt = widths[pos]\n",
    "                ans += 1\n",
    "            else :\n",
    "                cnt += widths[pos]\n",
    "        return [ans, cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        S = list(s)\n",
    "        for i in range(len(S)):\n",
    "            S[i] = widths[ord(S[i])-97]\n",
    "        a = [1,0]\n",
    "        n = 0\n",
    "        j = 0\n",
    "        while j < len(S):\n",
    "            if n + S[j]<=100:\n",
    "                n = n + S[j]\n",
    "                j = j + 1\n",
    "            else:\n",
    "                a[0] = a[0] + 1\n",
    "                n = 0\n",
    "        a[1] = n\n",
    "        return a\n",
    "\n",
    "\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        ans = [1, 0]\n",
    "        for i in range(len(s)):\n",
    "            ans[1] += widths[ord(s[i]) - ord(\"a\")]\n",
    "            if ans[1] <= 100:\n",
    "                continue\n",
    "            if ans[1] > 100:\n",
    "                ans[0] += 1\n",
    "                ans[1] = widths[ord(s[i]) - ord(\"a\")]\n",
    "                continue\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n",
    "        maxline=100\n",
    "        line,width=1,0\n",
    "        for a in s:\n",
    "            need=widths[ord(a)-ord('a')]\n",
    "            width+=need\n",
    "            if width>maxline:\n",
    "                line+=1\n",
    "                width=need\n",
    "        return (line,width)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
